package remotecontrol;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;

import onebeartoe.net.ServerConnection;

public class ThreadedServer extends Thread
{

    public ThreadedServer()
    {
        port = 9999;
    }

    public void run()
    {
        String errorMessage;
        try
        {
            serverSocket = new ServerSocket(port);
            InetAddress addr = InetAddress.getLocalHost();
            byte ipAddr[] = addr.getAddress();
            String ipAddrStr = "";
            for(int i = 0; i < ipAddr.length; i++)
            {
                if(i > 0)
                    ipAddrStr = (new StringBuilder(String.valueOf(ipAddrStr))).append(".").toString();
                ipAddrStr = (new StringBuilder(String.valueOf(ipAddrStr))).append(ipAddr[i] & 0xff).toString();
            }

            ip = ipAddrStr;
            String hostname = addr.getHostName();
            System.out.println((new StringBuilder(String.valueOf("Your Remote Control address is no longer available"))));
            String inet = serverSocket.getLocalSocketAddress().toString();
            InetAddress inetAddress = serverSocket.getInetAddress();
     //     System.out.println((new StringBuilder("remove controle available at: ")).append(inet).append(" - ").append(inetAddress.getCanonicalHostName()).append(" - ").append(inetAddress.getHostAddress()).append(" - ").append(inetAddress.getHostName()).toString());
            do
            {
                java.net.Socket client = serverSocket.accept();
                String inet2 = serverSocket.getLocalSocketAddress().toString();
                InetAddress inetAddress2 = serverSocket.getInetAddress();
    //            System.out.println((new StringBuilder("remove controle available at: ")).append(inet).append(" - ").append(inetAddress2.getCanonicalHostName()).append(" - ").append(inetAddress2.getHostAddress()).append(" - ").append(inetAddress2.getHostName()).toString());
                ServerConnection nextServerConnection = (ServerConnection)connection.clone();
                nextServerConnection.setClient(client);
                Thread request = new Thread(nextServerConnection);
                request.start();
            } while(true);
        }
        catch(IOException ioe)
        {
            errorMessage = ioe.toString();
        }
        System.out.println(errorMessage);
    }

    public void shutDown()
    {
        try
        {
            serverSocket.close();
            serverSocket = null;
        }
        catch(IOException ioe)
        {
            String errorMessage = ioe.toString();
            System.out.println(errorMessage);
        }
    }

    public void setPort(int port)
    {
        this.port = port;
    }

    public void setConnection(ServerConnection connection)
    {
        this.connection = connection;
    }

    public void setServerConnectionClass(String serverConnectionClass)
    {
        this.serverConnectionClass = serverConnectionClass;
    }

    private ServerSocket serverSocket;
    ServerConnection connection;
    String serverConnectionClass;
    public int port;
    public String ip;
}